package com.fengmi.service.impl;

import cn.hutool.json.JSONUtil;
import com.fengmi.api.goods.GoodsApi;
import com.fengmi.cart.Cart;
import com.fengmi.goods.MallGoodsSku;
import com.fengmi.interceptor.UserInfoInterceptor;
import com.fengmi.service.ICartService;
import com.fengmi.user.SysUser;
import com.fengmi.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 作者：铁蛋
 * 时间：2021/10/23 11:24
 * 描述：永无Bug
 */
@Service
public class CartServiceImpl implements ICartService {
    @Autowired
    private GoodsApi goodsApi;
    @Autowired
    private StringRedisTemplate template;
    @Override
    public ResultVO addCart(String skuId, Integer buyNum) {
        MallGoodsSku sku = goodsApi.findSkuById(skuId);
        if (sku ==null){
            return new ResultVO(false,"商品不存在");
        }
        if (buyNum>sku.getSkuStock()){
            return new ResultVO(false,"库存不足");
        }
        String userId= UserInfoInterceptor.getUserInfo().getUserId();
        BoundHashOperations<String, Object, Object> operations = template.boundHashOps("menhu:cart");
        Object cartList = operations.get(userId);
        if (StringUtils.isEmpty(cartList)){
            //购物车信息
            Cart cart=new Cart();
            cart.setBuyNum(buyNum);
            cart.setImg(sku.getSkuImage());
            cart.setMarketPrice(sku.getSkuMarketPrice().doubleValue());
            cart.setPrice(sku.getSkuPrice().doubleValue());
            cart.setSkuId(sku.getSkuId()+"");
            cart.setTitle(sku.getSkuTitle());
            cart.setSpec(sku.getSkuSpec());
            List<Cart> carts = Arrays.asList(cart);

            operations.put(userId, JSONUtil.toJsonStr(carts));
            return new ResultVO(true,"加入购物车成功");
        }

        List<Cart> carts = JSONUtil.toList((String) cartList, Cart.class);
        Long collect = carts.stream().filter(cart -> skuId.equals(cart.getSkuId())).collect(Collectors.counting());
        if (collect>=1){
            carts.forEach(cart -> {
                if (cart.getSkuId().equals(skuId)){
                    //修改购买数量
                    cart.setBuyNum(cart.getBuyNum()+buyNum);
                }
            });
        }else {
            //购物车信息
            Cart cart=new Cart();
            cart.setBuyNum(buyNum);
            cart.setImg(sku.getSkuImage());
            cart.setMarketPrice(sku.getSkuMarketPrice().doubleValue());
            cart.setPrice(sku.getSkuPrice().doubleValue());
            cart.setSkuId(sku.getSkuId()+"");
            cart.setTitle(sku.getSkuTitle());
            cart.setSpec(sku.getSkuSpec());
            carts.add(cart);
        }


        operations.put(userId,JSONUtil.toJsonStr(carts));


        return new ResultVO(true,"加入购物车成功");
    }

    @Override
    public List<Cart> findCartList() {
        SysUser userInfo =UserInfoInterceptor.getUserInfo();
        BoundHashOperations<String, Object, Object> operations = template.boundHashOps("menhu:cart");
        Object cartListJson = operations.get(userInfo.getUserId());
        return  JSONUtil.toList((String) cartListJson,Cart.class);
    }
}
